<?php

namespace app\order\logic;
use ba\Filesystem;

class OrderBtLogic
{

    const BT_KEY  = 'A7Mb0eZaYSgOSp7ihcsbghhY40oexmrk';
    const BT_PANEL= 'http://47.238.232.27:31107';

    //点击任务
    public function getCrontab(){
        //拼接URL地址
        $url = self::BT_PANEL.'/crontab?action=StartTask';

        //准备POST数据
        $p_data       = $this->GetKeyData();		//取签名
        $p_data['id'] = 2;
        //请求面板接口
        $result = $this->HttpPostCookie($url,$p_data);


        //解析JSON数据
        return json_decode($result,true);
    }


    /**
     * 构造带有签名的关联数组
     */
    private function GetKeyData(){
        return [
            'request_token'	=>	md5(time().''.md5(self::BT_KEY)),
            'request_time'	=>	time()
        ];
    }


    /**
     * 发起POST请求
     * @param String $url
     * @param Array|String $data
     * @return string
     */
    private function HttpPostCookie($url, $data, $timeout = 60)
    {
        $local_dir = app()->getRootPath() . 'public/storage/mqCrontab/';
        if (!is_dir($local_dir)) {
            mkdir($local_dir,0777 ,true);
        }
        $cookie_file = $local_dir . md5(self::BT_PANEL) . '.cookie';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }




}